
/**
 * Package:com.hysonic.scgl.service;
 * $Id: PaginationBean.java,v 1.1.1.1 2009/03/23 09:21:18 zhangpei Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.util;
import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * PaginationBean
 * <p/>
 * <p><a href="PaginationBean.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: zhangpei $</a>
 * @version $Revision: 1.1.1.1 $
 */
public class PaginationBean implements Serializable {
    private Log log = LogFactory.getLog(getClass());
    private int current=1;
    private int length=20;
    private int totalResult;
    private int showLevel;
    private boolean show = true;

    public PaginationBean() {
    }

    public PaginationBean(int length) {
        this.length = length;
    }

    public int getStart(){
        return (current-1)*length;
    }
    
    /**
     * 末条记录
     * @return 末条记录的索引
     */
    public int getEnd(){
    	int end = getStart()+length;
    	return end > getTotalResult() ? end : getTotalResult();
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public boolean getShow() {
        return show;
    }

    public void setShow(boolean show) {
        this.show = show;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getTotalResult() {
        return totalResult;
    }

    public void setTotalResult(int totalResult) {
        this.totalResult = totalResult;
    }
    public void setTotalResult(long totalResult) {
        this.totalResult = (int)totalResult;
    }

    public int getTotalPages() {
        return (int)Math.ceil(((float)totalResult)/length);
    }

    public int getPrevious(){
        int pre = current-1;
        if(pre <= 0){
         pre=1;
        }
        return pre ;
    }

     public int getPreviousBlock(){
        int pre = current-5;
        if(pre <= 0){
         pre=1;
        }
        return pre ;
    }

    public int getPageStart(){
        int pre = current-5;
         if(getTotalPages()-pre<10){
            pre  = getTotalPages()-9;   
         }
        if(pre <= 0){
         pre=1;
        }
        return pre ;
    }

    public int getNext(){
        int next = current + 1;
        if(next >= getTotalPages()){
           next = getTotalPages();
        }
        return next;
    }

    public int getNextBlock(){
        int next = current + 5;
        if(next >= getTotalPages()){
           next = getTotalPages();
        }
        return next;
    }

    public int getPageEnd(){
        int next = current + 4;
        if(current+5<10){
            next = 10;   
        }
        if(next >= getTotalPages()){
           next = getTotalPages();
        }
        return next;
   }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final PaginationBean that = (PaginationBean) o;

        if (current != that.current) return false;
        if (show != that.show) return false;
        if (length != that.length) return false;
        if (showLevel != that.showLevel) return false;
        if (totalResult != that.totalResult) return false;
        if (log != null ? !log.equals(that.log) : that.log != null) return false;

        return true;
    }


    public String toString() {
        return "PaginationBean{" +
                "current=" + current +
                ", length=" + length +
                ", totalResult=" + totalResult +
                ", showLevel=" + showLevel +
                ", show=" + show +
                '}';
    }


}
